<?php

namespace app\enterprise\controller;

use app\enterprise\model\department\Department as departmentModel;
use think\exception\ValidateException;

class Department extends Enterprise
{
    public function getDepartmentList()
    {
        $param = $this->request->param();
        $where = [];

        $user_pid = $this->adminInfo['pid'];

        if (!empty($param['department_name'])) {
            $where[] = ['department_name', 'like', '%' . $param['department_name'] . '%'];
        }
        if (!empty($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }

        $list = departmentModel::getList($where);
        if (empty($param['department_name']) && empty($param['status'])) {
            $list1 = getCategoryWithChildren($list, $user_pid);
//            $list = arrayToTree($list1, $user_pid);

            foreach ($list1 as $k => &$v) {
                if ($v['id'] == $user_pid) {
                    $v['children'] = arrayToTree($list1, $user_pid);
                } else {
                    unset($list1[$k]);
                }
            }
            unset($v);
            $list = $list1;
        }

        return app('json')->success([
            'list' => $list
        ]);
    }

    public function getAllDepartmentList()
    {
        return app('json')->success([
            'list' => departmentModel::getAlllist(['company_id' => $this->adminId])
        ]);
    }

    public function saveDepartment()
    {
        $param = $this->request->param();
        $param['company_id'] = $this->adminInfo['company_id'];

        try {
            validate([
                'pids' => 'require',
                'email|邮箱'=>'email',
                'link_mobile|联系人手机号'=>'regex:/^1[3-9]\d{9}$/',
                'department_name|部门名称' => 'require|max:255',
                'sort|排序' => 'number'
            ])->check($param);

            if ( is_array($param['pids']) ) {
                $param['pid'] = end($param['pids']);
            } else {
                $param['pid'] = $param['pids'];
            }

            $param['update_time'] = time();
            $departmentModel = new departmentModel();
            if (empty($param['id'])) {
                $param['create_time'] = time();
                $departmentModel->add($param);
            } else {
                unset($param['create_time']);
                $departmentModel->edit($param);
            }
            return app('json')->success('操作成功');
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }
    }

    public function departmentInfo()
    {
        $id = $this->request->param('id');
        return app('json')->success([
            'info' => departmentModel::where('id', '=', $id)->where('company_id' , '=', $this->adminId)
                ->where('status', '>', 0)
                ->find()
        ]);
    }

    public function deleteDepartment()
    {
        $id = $this->request->param('id');
        $departmentModel = new departmentModel();
        $departmentCount = $departmentModel->where('pid', $id)->where('status', '=',1)->count();
        if ($departmentCount > 0) {
            return app('json')->fail('本部门下还有子部门');
        }
        $departmentModel->setDelete(['id' => $id]);
        return app('json')->success('删除成功');
    }
}